package com.distributer.gateway.hander;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.JedisPool;

import com.distributer.gateway.model.RequestContext;

/**
 * 轮询算法获取请求地址
 * 使用redis维护一个全局的服务列表，一个全局的计数器。
 * 算法：计数器初始值为0，每次获取服务的算法为计数器%服务列表长度，然后计数器加1.
 * @author xiaolin
 *
 */
@Component
public class RobinRoteStategy implements RoteStrategy{

	@Autowired
	private JedisPool jedisPool;
	
	@Override
	public String getReallyRequestUrl(RequestContext requestContext) {
		
		return null;
	}
	
	public static void main(String[] args) {
		int i = 0;
		int s1Count = 0;
		int s2Count = 0;
		int s3Count = 0;
		String[] serviceList = {"s1","s2","s3"};
		for (int j = 0; j < 100000; j++) {
			int index =  i % serviceList.length;
			String serviceStr = serviceList[index];
			if ("s1".equals(serviceStr)) {
				s1Count ++;
			}
			if ("s2".equals(serviceStr)) {
				s2Count ++;
			}
			if ("s3".equals(serviceStr)) {
				s3Count ++;
			}
			i ++;
		}
		System.out.println("s1="+s1Count+";s2="+s2Count+";s3="+s3Count);
 	}

}
